import * as path from 'path';
import * as Moment from 'moment';
const baseLogPath = path.resolve(__dirname, '../logs'); // 日志要写入哪个目录

const log4jsConfig = {
  appenders: {
    console: {
      type: 'console', // 会打印到控制台
    },
    http: {
      type: 'dateFile', // 会写入文件，并按照日期分类
      filename: `${baseLogPath}/${Moment().format('YYYY-MM-DD')}/info.log`, // 日志文件名，会命名为：access.20200320.log
      alwaysIncludePattern: true,
      pattern: 'yyyy-MM-dd',
      layout: {
        type: 'pattern',
        pattern: `[${Moment().format('YYYY-MM-DD HH:mm:ss')}]:'%m`,
      },
      daysToKeep: 60,
      numBackups: 3,
      category: 'http',
      keepFileExt: true, // 是否保留文件后缀
    },
    app: {
      type: 'dateFile',
      filename: `${baseLogPath}/${Moment().format('YYYY-MM-DD')}/app.log`,
      alwaysIncludePattern: true,
      layout: {
        type: 'pattern',
        pattern: `[${Moment().format('YYYY-MM-DD HH:mm:ss')}]:'%m`,
      },
      // 日志文件按日期（天）切割
      pattern: 'yyyy-MM-dd',
      daysToKeep: 60,
      // maxLogSize: 10485760,
      numBackups: 3,
      keepFileExt: true,
    },
    sql: {
      type: 'dateFile',
      filename: `${baseLogPath}/${Moment().format('YYYY-MM-DD')}/sql.log`,
      alwaysIncludePattern: true,
      layout: {
        type: 'pattern',
        pattern: `[${Moment().format('YYYY-MM-DD HH:mm:ss')}]:'%m`,
      },
      // 日志文件按日期（天）切割
      pattern: 'yyyy-MM-dd',
      daysToKeep: 60,
      numBackups: 3,
      keepFileExt: true,
    },
    errorFile: {
      type: 'dateFile',
      filename: `${baseLogPath}/${Moment().format('YYYY-MM-DD')}/error.log`,
      alwaysIncludePattern: true,
      layout: {
        type: 'pattern',
        pattern: `[${Moment().format('YYYY-MM-DD HH:mm:ss')}]:'%m`,
      },
      // 日志文件按日期（天）切割
      pattern: 'yyyy-MM-dd',
      daysToKeep: 60,
      // maxLogSize: 10485760,
      numBackups: 3,
      keepFileExt: true,
    },
    errors: {
      type: 'logLevelFilter',
      level: 'ERROR',
      appender: 'errorFile',
    },
  },
  categories: {
    default: {
      appenders: ['console', 'errors'],
      level: 'DEBUG',
    },
    sql: { appenders: ['console', 'sql'], level: 'info' },
    error: { appenders: ['console', 'errors'], level: 'info' },
    info: { appenders: ['console', 'http'], level: 'info' },
    access: { appenders: ['console', 'errors'], level: 'info' },
    http: { appenders: ['http'], level: 'DEBUG' },
  },
  //   pm2: true, // 使用 pm2 来管理项目时，打开
  //   pm2InstanceVar: 'INSTANCE_ID', // 会根据 pm2 分配的 id 进行区分，以免各进程在写日志时造成冲突
};

export default log4jsConfig;
